package view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Logout")
public class LogoutServlet extends Servlet {

	private static final long serialVersionUID = -7848627383874227367L;

	@Override
	void fillGetContent(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		HttpSession session = request.getSession(false);

		boolean wasLogged = false;

		if (session != null) {
			if (session.getAttribute("author") != null) {
				wasLogged = true;
				session.setAttribute("author", null);
			}
		}

		if (wasLogged) {
			try {
				response.sendRedirect("Controller?button=Pesquisar");
			} catch (IOException e) {
				out.println(HtmlFactory.createErrorMessage(e.getMessage()));
			}
			out.println(HtmlFactory.createSuccessMessage("Até a próxima!"));
		} else {
			out.println(HtmlFactory.createErrorMessage("Você não está logado(a)!"));
		}

	}

	@Override
	void fillPostContent(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		out.println(HtmlFactory.createErrorMessage("Error 404. Page not found."));
	}

}
